<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/22
 * Time: 15:49
 */
namespace app\index\controller;
use app\index\model\Account;
use app\index\model\Manager;
use think\App;
use think\Request;
use think\facade\Session;
class Jxcaccount extends Common
{
    public function __construct(App $app = null, Request $request)
    {
        parent::__construct($app, $request);
    }


    //查询用户信息
    public function getone(){
        $post=$this->param;
        if (!empty($post['moblie'])) {
            $re=Account::getone(array('title' => $post['moblie']));
            if ($re) {
                $data['code']='1001';
                $data['msg']='';
                $data['data']=$re;
            }else{
                $data['code']='1002';
                $data['msg']='未查询到该用户';
            }
        }else{
            $data['code']='1002';
            $data['msg']='用户账号不能为空';
        }
        $this->return = $data;
    }
    //查询用户列表
    public function index(){
        $post=$this->param;
        $limit=10;
        $page=1;
        if (!empty($post['size'])) {
            $limit=$post['size'];
        }
        if (!empty($post['page'])) {
            $page=$post['page'];
        }
        $where=array('audit' => 1);
        if (!empty($post['moblie'])) {
            $where_m=array('moblie' => $post['moblie']);
            $where = array_merge($where,$where_m);
        }
        if (!empty($post['name'])) {
            $where_u=array('username' => $post['name']);
            $where = array_merge($where,$where_u);
        }
        if (!empty($post['sid'])) {
            $where_s=array('sid' => $post['sid']);
            $where = array_merge($where,$where_s);
        }
        $data['count']=Account::getcount($where);
        $data['data']=Account::getlist($where,$limit,$limit*($page-1));
        $this->return = $data;
    }
    //新建用户
    public function add(){
        $post=$this->param;
        if(empty($post['usercode'])){
            $data['code']='1002';
            $data['msg']='会员编号不能为空';
        }elseif (empty($post['moblie'])) {
            $data['code']='1002';
            $data['msg']='手机号不能为空';
        }elseif (empty($post['name'])) {
            $data['code']='1002';
            $data['msg']='会员姓名不能为空';
        }else{
            $add_data['uid']=Session::get("u_id");
            $add_data['sid']=Session::get("shop_id");
            $add_data['usercode']=$post['usercode'];
            $add_data['title']=$post['moblie'];
            $add_data['phone']=$post['moblie'];
            $add_data['sex']=$post['sex'];
            $add_data['birthday']=$post['birthday'];
            $add_data['occupation']=$post['occupation'];
            $add_data['utype']=$post['utype'];
            $add_data['timeline']=time();
            $user=Manager::getone(array('id' => Session::get("u_id")));
            $add_data['username']=$user['username'];
            $add_data['nick_name']=$post['name'];
            $re=Account::add($add_data);
            if ($re) {
                $data['code']='1001';
                $data['msg']='';
            }else{
                $data['code']='1002';
                $data['msg']='创建失败';
            }
        }
        $this->return = $data;
    }
    //修改用户信息
    public function updata(){
        $post=$this->param;
        if(empty($post['user_id'])){
            $data['code']='1002';
            $data['msg']='会员ID不能为空';
        }elseif (empty($post['moblie'])) {
            $data['code']='1002';
            $data['msg']='手机号不能为空';
        }elseif (empty($post['name'])) {
            $data['code']='1002';
            $data['msg']='会员姓名不能为空';
        }else{
            $update_data['title']=$post['moblie'];
            $update_data['phone']=$post['moblie'];
            $update_data['sex']=$post['sex'];
            $update_data['birthday']=$post['birthday'];
            $update_data['occupation']=$post['occupation'];
            $update_data['utype']=$post['utype'];
            $update_data['timeline']=time();
            $user=Manager::getone(array('id' => Session::get("u_id")));
            $update_data['username']=$user['username'];
            $update_data['nick_name']=$post['name'];
            $re=Account::updateacct($update_data,array('id' => $post['user_id']));
            if ($re) {
                $data['code']='1001';
                $data['msg']='';
            }else{
                $data['code']='1002';
                $data['msg']='创建失败';
            }
        }
        $this->return = $data;
    }
    //删除用户
    public function del(){
        $post=$this->param;
        if(empty($post['user_id'])){
            $data['code']='1002';
            $data['msg']='会员ID不能为空';
        }else{
           $where=array('id' => $post['user_id']);
            $res=Account::deluser($where);
            if ($res) {
                $data['code']='1001';
                $data['msg']='';
            }else{
                $data['code']='1002';
                $data['msg']='参数错误';
            } 
        }
        $this->return = $data;
    }
}

